<template>
  <div class="view">
    <OperateC
        :top-level="topLevel"
        :add="addUser"
        :edit="editUser"
        :remove="removeUser"
        :add-show="this.$getButtonAuth('user:add')"
        :remove-show="this.$getButtonAuth('user:del:batch')"
    />
    <UserTable
        :top-level="topLevel"
        :edit-show="this.$getButtonAuth('user:edit')"
        :reset-show="this.$getButtonAuth('user:reset')"
        :del-show="this.$getButtonAuth('user:del')"
        :permission-show="this.$getButtonAuth('user:permission')"
        :forbidden-show="this.$getButtonAuth('user:forbidden')"
    />
    <UserDialog></UserDialog>
  </div>
</template>

<script>
import OperateC from "@/components/OperateC";
import UserTable from "@/components/table/UserTable";
import UserDialog from "@/components/dialog/UserDialog";
import {deleteUserByIds, getUsers} from "@/api/user";
export default {
  name: "UserView",
  components: {OperateC, UserTable, UserDialog},
  data() {
    return {
      topLevel: -1
    }
  },
  methods: {
    addUser() {
      // 打开添加dialog
      var params = {
        dialogTitle: "添加用户",
        dialogFormVisible: true
      }
      this.$store.commit("user/openDialogForm", params);
    },
    editUser() {
      var selected = this.$store.state.user.multipleSelection;
      if (selected.length < 1) {
        this.$message.warning("你还没有选中数据哦！");
        return;
      }
      if (selected.length > 1) {
        this.$message.warning("一次只能修改一条数据哦！")
        return;
      }
      this.$store.dispatch("user/editUser", selected[0]);
    },
    removeUser() {
      var selected = this.$store.state.user.multipleSelection;
      if (selected.length < 1) {
        this.$message.warning("请先选择要删除的数据哦！");
        return;
      }
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteUserByIds(selected).then((res) => {
          this.$message.success(res.data.msg);
          // 刷新
          var params = {
            "current": this.$store.state.user.currentPage,
            "size": this.$store.state.user.pageSize
          };
          getUsers(params).then((res) => {
            this.$store.state.user.tableData = res.data.data;
            this.$store.state.user.total = res.data.total;
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>

</style>
